<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 200px;
        height: 2000px;
        background-color: greenyellow;
        margin: 0 auto;
      }
      .box {
        width: 100px;
        height: 200px;
        background-color: cyan;
        position: absolute;
        top: 100px;
      }
    </style>
  </head>
  <body>
    <div></div>
    <div class="box"></div>

    <script>
      var divEle = document.querySelector("div");
      var boxEle = document.querySelector(".box");
      console.log(document.documentElement.scrollTop);
      window.onscroll = function fn() {
        if (document.documentElement.scrollTop >= 200) {
          boxEle.style.position = "fixed";
          boxEle.style.top = "-0px";
        } else {
          boxEle.style.position = "absolute";
          boxEle.style.top = "100px";
        }
      };
    </script>
  </body>
</html>
